﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using BOHVote.BLL;
using System.Text;
using BOHVote.Entity;
 

namespace BOH.AccreditationSystem
{
    public partial class ApplyInterView : System.Web.UI.Page
    {
        private BHUser user;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] != null)
            {
                user = (BHUser)Session["User"];
            }
            if (!IsPostBack)
            {
                if (user != null)
                {
                     if (user.RightType == "G")
                    {
                        SpanSetup3.Visible = true;
                         SpanSetup.Visible = false;
                        //DataSetApplyList( "-1",user.GovernID);
                        UnitGovernBind(user.GovernID);
                        SpanSetup2.Visible = false;
                    }
                    else if (user.RightType == "C" || user.RightType == "A")
                    {
                        
                        UnitGovernBind("");
                        //DataSetApplyList("-1", "-1");
                        SpanSetup.Visible = true;
                        SpanSetup2.Visible = true;
                        SpanSetup3.Visible = true;
                        
                    }
                }
                BindJobRank();
                SubjectBind();
                bindTime();
                if (Session["ApplyPeriod"] != null)
                {
                    //ApplyListDataSet();
                    HasBuildNumber();
                }
                
            }
        }

        public void HasBuildNumber()
        {
            int count = ApplyClass.CountUnBuildNumber((string)Session["ApplyPeriod"]);
            if (count > 0)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "confirm", "<script>alert('" + (string)Session["ApplyPeriod"] + "年度 申报编号未生成! ');</script>");
            }
        }

        public void UnitGovernBind(string GovernID)
        {
            DataSet dstGovern = null;
            if (((BHUser)Session["User"]).RightType == "C" || ((BHUser)Session["User"]).RightType == "A")
            {
                ListItem list = new ListItem("请选择主管局", "-1");
                drpGovern.Items.Add(list);
            }
            dstGovern = UnitClass.getGovernByGovernID(GovernID);

            if (dstGovern.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in dstGovern.Tables[0].Rows)
                {
                    if (row["GovernID"].ToString().Trim().Length != 0)
                    {
                        ListItem li = new ListItem(row["GovernName"].ToString(), row["GovernID"].ToString());
                        this.drpGovern.Items.Add(li);
                        //this.bindDropChildItem(row["GovernID"].ToString());
                    }
                }
            }
        }

        public void BindJobRank()
        {
            drpJobRank.Items.Clear();
            ListItem li1 = new ListItem("请选择申报职称", "-1");
            this.drpJobRank.Items.Add(li1);
            DataSet dst = OtherClass.getJobRank();
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                ListItem li = new ListItem(dr["JobRankName"].ToString(), dr["JobRankID"].ToString());
                drpJobRank.Items.Add(li);
            }
        }

        public void SubjectBind()
        {
            this.drpSubject.Items.Clear();
            DataSet dst = Subject.getAllASubject();
            ListItem li1 = new ListItem("请选择学科组", "-1");
            this.drpSubject.Items.Add(li1);
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                if (dr["SubjectID"].ToString().Length > 0)
                {
                    ListItem li = new ListItem(dr["SubjectName"].ToString(), dr["SubjectID"].ToString());
                    this.drpSubject.Items.Add(li);
                }
            }
        }

        //protected void AspNetPager1_PageChanged(object src, EventArgs e)
        //{
        //    ApplyListDataSet();
        //}

        public DataSet dstApplyList(string SubjectID, string JobRank,string GovernID, string ApplyPeriod)
        {
            DataSet dst = null;
            string UnitGovrenID = string.Empty;

            dst = ApplyClass.dstApplysForInterview(string.Empty,"-1","-1", GovernID, "-1", "-1", "-1", SubjectID, JobRank, ApplyPeriod);
            DataColumn cl = new DataColumn();
            cl.ColumnName = "isSet";
            dst.Tables[0].Columns.Add(cl);
            DataColumn clTime = new DataColumn();
            clTime.ColumnName = "InterviewDateTime";
            dst.Tables[0].Columns.Add(clTime);

            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                if (!string.IsNullOrEmpty(dr["InterViewID"].ToString()))
                {
                    dr["isSet"] = "已设置";
                }
                else
                {
                    dr["isSet"] = "未设置";
                }
                if (!string.IsNullOrEmpty(dr["InterView_Month"].ToString()) && !string.IsNullOrEmpty(dr["InterView_Day"].ToString()) && !string.IsNullOrEmpty(dr["interview_TimeHH"].ToString()) && !string.IsNullOrEmpty(dr["interview_TimeMM"].ToString()))
                {
                    dr["InterviewDateTime"] = dr["InterView_Month"].ToString() + "月" + dr["InterView_Day"].ToString() + "日" + " " + dr["interview_TimeHH"].ToString() + ":" + dr["interview_TimeMM"].ToString();
                }
                else
                {
                    dr["InterviewDateTime"] = string.Empty;
                }
            }
            return dst;
        }

        public void ApplyListDataSet()
        {
            string GovernID = "-1";
            if (!String.IsNullOrEmpty(((BHUser)Session["User"]).GovernID) && ((BHUser)Session["User"]).RightType == "G")
                GovernID = ((BHUser)Session["User"]).GovernID;
            else
                GovernID = drpGovern.SelectedValue;
            DataSet dst = dstApplyList(drpSubject.SelectedValue, drpJobRank.SelectedValue,GovernID, (string)Session["ApplyPeriod"]);
            StringBuilder sbPrintApply = new StringBuilder();
            foreach (DataRow dr in dst.Tables[0].Rows)
            {
                sbPrintApply.Append(dr["ApplyID"].ToString() + ";");
            }
            //hidePrintApplyIDList.Value = sbPrintApply.ToString();
            BindApplyList(dst);
        }

        public void BindApplyList(DataSet dst)
        {
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                ApplyViewList.DataSource = dst;
                ApplyViewList.DataBind();

                /*AspNetPager1.RecordCount = dst.Tables[0].Rows.Count;
                if (AspNetPager1.RecordCount != 0)
                {
                    labNoRecord.Visible = false;
                    AspNetPager1.Visible = true;
                    PagedDataSource pds = new PagedDataSource();
                    pds.DataSource = dst.Tables[0].DefaultView;
                    pds.AllowPaging = true;
                    pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                    pds.PageSize = AspNetPager1.PageSize;
                    ApplyViewList.DataSource = pds;
                    ApplyViewList.DataBind();
                }
                else
                {
                    ApplyViewList.DataSource = null;
                    ApplyViewList.DataBind();
                    AspNetPager1.Visible = false;
                    labNoRecord.Visible = true;
                }*/
            }
            else
            {
                //AspNetPager1.Visible = false;
                labNoRecord.Visible = true;
                ApplyViewList.DataSource = null;
                ApplyViewList.DataBind();
            }
        }

        protected void butSearch_Click(object sender, EventArgs e)
        {
            ApplyListDataSet();
        }

        public void BindMonth(ListControl listControl)
        {
            listControl.Items.Clear();
            ListItem list2 = new ListItem("", "");
            listControl.Items.Add(list2);
            for (int i = 1; i <= 12; i++)
            {

                ListItem list1 = new ListItem(i.ToString("00"), i.ToString("00"));
                listControl.Items.Add(list1);
            }
        }

        public void bindTime()
        {
            BindMonth(drpInteviewMonth);
            BindMM(drpMM);
            bindHH(drpHH);
            BindDay(drpInteviewday);
        }

        public void BindDay(ListControl listControl)
        {
            listControl.Items.Clear();
            ListItem list2 = new ListItem("", "");
            listControl.Items.Add(list2);
            for (int i = 1; i <= 31; i++)
            {
                ListItem list1 = new ListItem(i.ToString("00"), i.ToString("00"));
                listControl.Items.Add(list1);
            }
        }

        public void BindMM(ListControl listControl)
        {
            listControl.Items.Clear();
            ListItem list2 = new ListItem("", "");
            listControl.Items.Add(list2);
            for (int i = 0; i < 60; i++)
            {

                ListItem list1 = new ListItem(i.ToString("00"), i.ToString("00"));
                listControl.Items.Add(list1);
            }
        }

        public void bindHH(ListControl listControl)
        {
            listControl.Items.Clear();
            ListItem list2 = new ListItem("", "");
            listControl.Items.Add(list2);
            for (int i = 0; i < 24; i++)
            {
                ListItem list1 = new ListItem(i.ToString("00"), i.ToString("00"));
                listControl.Items.Add(list1);
            }
        }

        protected void butSetDateTime_Click(object sender, EventArgs e)
        {
            try
            {
                bool hasSuccess = true;
                hasSuccess = ApplyClass.UpdateAllInterview(drpSubject.SelectedValue, drpJobRank.SelectedValue, (string)Session["ApplyPeriod"], drpInteviewMonth.SelectedValue, drpInteviewday.SelectedValue, drpHH.SelectedValue, drpMM.SelectedValue);
                if (hasSuccess)
                {
                    ApplyListDataSet();
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert(面试时间设置失败);</script>");
                }

            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert(面试时间设置失败);</script>");
            }
        }

        protected void butPrint_Click(object sender, EventArgs e)
        {
            
           // Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "printPage", "<script>ShowPrintPage();</script>");
        }

        protected void ApplyViewList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (user != null)
                {
                    if (user.RightType == "G")
                    {
                        ApplyViewList.Columns[7].Visible = false;
                    }
                }

                GridViewRow gvr = (GridViewRow)e.Row;
                DropDownList drpInteviewMonth = (DropDownList)gvr.FindControl("drpInteviewMonth");
                DropDownList drpInteviewday = (DropDownList)gvr.FindControl("drpInteviewday");
                DropDownList drpHH = (DropDownList)gvr.FindControl("drpHH");
                DropDownList drpMM = (DropDownList)gvr.FindControl("drpMM");
                BindMonth(drpInteviewMonth);
                BindMM(drpMM);
                bindHH(drpHH);
                BindDay(drpInteviewday);
            }
        }

        protected void ApplyViewList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridViewRow gvr = (GridViewRow)e.Row;
                DropDownList drpInteviewMonth = (DropDownList)gvr.FindControl("drpInteviewMonth");
                DropDownList drpInteviewday = (DropDownList)gvr.FindControl("drpInteviewday");
                DropDownList drpHH = (DropDownList)gvr.FindControl("drpHH");
                DropDownList drpMM = (DropDownList)gvr.FindControl("drpMM");
                string strMonth = ((HiddenField)gvr.FindControl("hideMonth")).Value;
                string strDay = ((HiddenField)gvr.FindControl("hideday")).Value;
                string strHH = ((HiddenField)gvr.FindControl("hidenHH")).Value;
                string strMM = ((HiddenField)gvr.FindControl("hideMM")).Value;
                drpInteviewMonth.SelectedValue = strMonth;
                drpInteviewday.SelectedValue = strDay;
                drpHH.SelectedValue = strHH;
                drpMM.SelectedValue = strMM;
            }
        }

        protected void butSaveTime_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow gvr in ApplyViewList.Rows)
            {
                DropDownList drpInteviewMonth = (DropDownList)gvr.FindControl("drpInteviewMonth");
                DropDownList drpInteviewday = (DropDownList)gvr.FindControl("drpInteviewday");
                DropDownList drpHH = (DropDownList)gvr.FindControl("drpHH");
                DropDownList drpMM = (DropDownList)gvr.FindControl("drpMM");
                string InterViewID  = ((HiddenField)gvr.FindControl("hideInterviewID")).Value;
                string ApplyID = ((HiddenField)gvr.FindControl("hidenApplyID")).Value;
                if (!string.IsNullOrEmpty(drpInteviewMonth.SelectedValue) && !string.IsNullOrEmpty(drpInteviewday.SelectedValue) && !string.IsNullOrEmpty(drpHH.SelectedValue) && !string.IsNullOrEmpty(drpMM.SelectedValue))
                {
                    ApplyClass.UpdateSingleInterview(ApplyID, InterViewID, (string)Session["ApplyPeriod"], drpInteviewMonth.SelectedValue, drpInteviewday.SelectedValue, drpHH.SelectedValue, drpMM.SelectedValue);
                    ApplyListDataSet();
                }

            }
        }

        protected void ApplyViewList_DataBound(object sender, EventArgs e)
        {
            if (((BHUser)Session["User"]) != null && ((BHUser)Session["User"]).RightType == "G")
            {
                ApplyViewList.Columns[9].Visible = false;
            }
            else
            {
                ApplyViewList.Columns[9].Visible = true;
            }
        }
    }
}
